package com.qzyh.esb.egsa.interceptor;

import com.qzyh.esb.egsa.singleton.SeqNoSingleton;
import org.apache.logging.log4j.ThreadContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class UrlInterceptor implements HandlerInterceptor {
    //通过LogFactory获取Logger类的实例
    private static final Logger logger = LoggerFactory.getLogger(UrlInterceptor.class);

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //生成traceId，用于追踪一笔交易全流程
        int seqNo = SeqNoSingleton.getInstance().getSeqNo();
        String traceId = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())+"-"+seqNo;
        ThreadContext.put("traceId",traceId);
        String uri = request.getRequestURI();
        logger.info("收到的请求url："+uri);
        return true;
    }
}
